<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="res/layui/css/layui.css">
<script src="res/js/jquery-2.1.4.min.js" type="text/javascript"></script>
<script src="res/layui/layui.js"></script>
<style type="text/css">
	td{
		text-align: center;
	}
	th{
		text-align: center;
	}
</style>
</head>
<body>
	<div style="margin: auto;margin-top:20px; width: 80%; border: 1px solid gray;">
	<form class="layui-form">
	<div class="layui-block" style="margin-top:10px;display: block;">
      <label class="layui-form-label">选择材料</label>
      <div class="layui-input-inline">
        <select name="dosId" lay-verify="required" lay-search="">
          <option value="">选择数据</option>
        </select>
      </div>
      <hr>
      <div class="layui-inline">
    <label class="layui-form-label">所需重量</label>
    <div class="layui-input-inline">
      <input type="text" name="dosCount" lay-verify="required" placeholder="请输入重量（克）" autocomplete="off" class="layui-input">
    </div>
  </div>
  <input type="button" class="layui-btn layui-btn-normal" onclick="addTable()" value="添加材料"/>
    </div>
    </form>
		<table class="layui-table" id="batching" style="margin-bottom:50px; margin-top: 20px;">
			<thead>
				<tr>
				<th style="text-align: center;">所需配料</th>
				<th style="text-align: center;">配料重量（克/g）</th>
				<th style="text-align: center;">操作</th>
				</tr>
			</thead>
			
		</table>
		<div style="margin-left: 80%; margin-bottom: 20px; "><a href="javascript:add();" class="layui-btn layui-btn-lg layui-btn-primary layui-btn-radius">保存</a></div>
	</div>
	<script type="text/javascript">
	var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
	$(function(){
		loadPlList()
		$.ajax({
			type: "POST",
			url: "dosing/selectAll.action?page=1&limit=999999",
			data: null,
			async: false,
			error: function(){
				layer.mes("发生未知错误，请稍后再试！");
			},
			success: function(obj){
				var dosList=obj.data;
				for(var i=0;i<dosList.length;i++){
					var dos=dosList[i];
					$("[name=dosId]").append("<option value="+dos.dosId+">"+dos.dosName+"</option>")
				}
			}
		});
	})
	function add(){
		var str="";
		$($("#batching tr")).each(function(){
			$(this).children("td").each(function(){
				str+=$(this).attr('class')+"="+$(this).text()+","
				$(this).children("input").each(function(){
					str+=$(this).attr('class')+"="+$(this).val()+"_";
				})
			})
		})
		if(GetQueryString("wareId")!=null){
			if(str!=''&&str!=null){
				$.ajax({
	                type : "POST",
	                url : "ware/addDep.action?dep="+str+"&wareId="+GetQueryString("wareId"),
	                data : null,
	                cache : false,
	                success : function(mes){
	               	 parent.layer.msg(mes.mes);
						
						if(GetQueryString("wareId")!=""){
							$(".layui-laypage-skip .layui-laypage-btn",window.parent.document).click();//刷新父页面数据表格的当前页
						}else{
							parent.table.reload('typeId');
						}
						layer.close(loadIndex);//加载层关闭  
						parent.layer.close(index);
	                },
	                error :function(){ 
	               	layer.close(loadIndex);//加载层关闭  
	               	layer.msg("发生未知错误，请稍后再试！");
	                }
	         })
			}else{
				layer.msg('配料不能为空',{icon: 5,anim: 6}); 
			}
		}else{
			parent.$("#plList").val(str);
		}
		parent.layer.close(index);//加载层关闭 
	}
	
	function addTable(){
		var isE=false;
		if($("[name=dosId]").val()!=''&&$("[name=dosCount]").val()!=''){
			$("tr").children("td").each(function(){
				$(this).children("input").each(function(){
					if(isE){
						return;
					}
					if($(this).val()==$("[name=dosId]").val()){
						var num=parseFloat($(this).parent().text())+parseFloat($("[name=dosCount]").val());
						$(this).parent().html(num+"<input type='hidden' value="+$("[name=dosId]").val()+">");
						isE=true;
					}
					
				})
			})
			if(!isE){
				$("#batching").append("<tr><td class='dosName'>"+$("[name=dosId] option:selected").text()+"</td><td class='dosCount'>"+$("[name=dosCount]").val()+"<input class='dosId' type='hidden' value='"+$("[name=dosId]").val()+"'></td><th><button onclick='deletes(this)' class='layui-btn layui-btn-sm'><i class='layui-icon'></i></button></th></tr>")
			}
		}else{
			layer.msg('数量或材料不能为空',{icon: 5,anim: 6}); 
		}
		$("[name=dosId]")[0].options.selectedIndex = 0;
		$("[name=dosCount]").val("");
		form.render();
	}
	var form;
	layui.use(['form'], function() {
		form = layui.form;
		
		
	});
	function loadPlList(){
		var str=GetQueryString("pl");
	 	var plList = str.split("_");
	 	
	 	for(var i=1;i<plList.length;i++){
	 		var objList=plList[i-1].split(",");
	 		var body="<tr>";
	 		for(var j=0;j<objList.length-1;j++){
	 			var obj=objList[j].split("=");
	 			if(j==1){
		 			body+="<td class="+obj[0]+">"+obj[1]+"<input class="+objList[j+1].split("=")[0]+" type='hidden' value="+objList[j+1].split("=")[1]+" /></td><th><button onclick='deletes(this)' class='layui-btn layui-btn-sm'><i class='layui-icon'></i></button></th>"
		 			j++;
		 		}else{
		 			body+="<td class="+obj[0]+">"+obj[1]+"</td>";
		 		}
	 		}
	 		body+="</tr>";
	 		$("#batching").append(body);
	 		
	 		
	 		
	 		
	 		/* if(i%3==1){
	 			body+="<tr>";
	 		}
	 		if(i%3==2){
	 			body+="<td class="+obj[0]+">"+obj[1]+"<input class="+plList[i].split("=")[0]+" type='hidden' value="+plList[i].split("=")[1]+" /></td>"
	 			i++;
	 		}else{
	 			body+="<td class="+obj[0]+">"+obj[1]+"</td>";
	 		}
	 		
	 		if(i%3==0){
	 			body+="<th><button onclick='deletes(this)' class='layui-btn layui-btn-sm'><i class='layui-icon'></i></button></th></tr></tr>";
	 		} */
	 		}
	 	
	 	
		
	}
	function deletes(obj){
		$(obj).parents("tr").remove();
	}
	/**
	得到URL栏的参数 
	*/
	function GetQueryString(name){
	     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	     var r = window.location.search.substr(1).match(reg);
	     if(r!=null)return  decodeURI(r[2]); return null;
	}
	</script>
</body>
</html>